package com.yeqiangwei.commons.image;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.yeqiangwei.commons.io.FileHelper;
import com.yeqiangwei.commons.util.ArithHelper;
import com.yeqiangwei.commons.util.TypeChange;

import magick.ImageInfo;
import magick.MagickException;
import magick.MagickImage;

public class ImageMagic extends ImageOperate {

	/*private static final Log log = LogFactory.getLog(ImageMagic.class);

	public static void main(String args[]) throws IOException {
		System.out.println(System.getProperty("java.library.path"));
		String opath = "/home/aroot/Life/a.jpg";
		String npath = "/home/aroot/Life/b.jpg";
		ImageOperate io = new ImageMagic();
		boolean b = io.setSquare(opath, npath);
		System.out.println(b);
	}

	*//**
	 * 设置成正方形
	 * 
	 * @param opath
	 * @param npath
	 * @param width
	 * @param height
	 *//*
	public boolean setSquare(String opath, String npath) {
		System.setProperty("jmagick.systemclassloader", "no");
		ImageInfo info = null;
		MagickImage image = null;
		try {
			info = new ImageInfo(opath);
			image = new MagickImage(info);
		} catch (MagickException e) {
			System.out.println(e);
		}
		if(info == null){
			System.out.println("[EEE] Failed to initialize ImageInfo");
			log.error("[EEE] Failed to initialize ImageInfo");
			return false;
		}
		if(image == null){
			System.out.println("[EEE] Failed to initialize MagickImage");
			log.error("[EEE] Failed to initialize MagickImage");
			return false;
		}
		try {
			Dimension dim = image.getDimension();
			double w = dim.getWidth();
			double h = dim.getHeight();
			if (w != h) {
				Rectangle rect = null;
				if (w > h) {
					// 高宽差
					double i = w - h;
					rect = new Rectangle(TypeChange.doubleToInt(i / 2), 0
							, TypeChange.doubleToInt(h), TypeChange.doubleToInt(h));
				} else if (w < h) {
					rect = new Rectangle(0, 0, TypeChange.doubleToInt(w)
							, TypeChange.doubleToInt(w));
				}
				MagickImage cropped = image.cropImage(rect);
				cropped.setFileName(npath);
				cropped.writeImage(info);
			} else {
				FileHelper.copyFile(opath, npath);
			}
			return true;
		} catch (Exception e) {
			log.error(e);
			System.out.println(e);
		}
		return false;
	}
	
	public boolean zoomIn(String opath, String npath, int w, int h){
		
		return false;
	}
	
	public boolean zoomOut(String opath, String npath, int w, int h){
		System.setProperty("jmagick.systemclassloader", "no");
		try {
			ImageInfo info = new ImageInfo(opath);
			MagickImage image = new MagickImage(info);
			Dimension dim = image.getDimension();
			double ow = dim.getWidth();
			double oh = dim.getHeight();
			if(w == 0 && h ==0){
				 如果都等于0则不执行任何操作 
			}
			else if(w > 0 && h > 0){
				return this.resize(opath, npath, w, h);
			}
			else if(h == 0){
				if(w < ow){
					double d = ArithHelper.div(w, ow);  指定尺寸/源尺寸 
					h = TypeChange.doubleToInt(ArithHelper.mul(oh, d));
				}else{
					com.yeqiangwei.commons.io.FileHelper.copyFile(opath, npath);
					return true;
				}
			}
			else if(w == 0){
				if(h < oh){
					double d = ArithHelper.div(h, oh);  指定尺寸/源尺寸 
					w = TypeChange.doubleToInt(ArithHelper.mul(ow, d));
				}else{
					com.yeqiangwei.commons.io.FileHelper.copyFile(opath, npath);
					return true;
				}
			}
			if(w <= 0){
				w = TypeChange.doubleToInt(ow);
			}
			if(h <= 0){
				h = TypeChange.doubleToInt(oh);
			}
			return this.resize(opath, npath, w, h);
		} catch (MagickException e) {
			log.error(e.toString());
		} catch (IOException e) {
			log.error(e.toString());
		}
		return false;
	}

	*//**
	 * 成比例缩放图片
	 *//*
	public boolean scaled(String opath, String npath, int w, int h) {
		System.setProperty("jmagick.systemclassloader", "no");
		try {
			ImageInfo info = new ImageInfo(opath);
			MagickImage image = new MagickImage(info);
			Dimension dim = image.getDimension();
			double ow = dim.getWidth();
			double oh = dim.getHeight();
			if(w == 0 && h ==0){
				 如果都等于0则不执行任何操作 
			}
			else if(w > 0 && h > 0){
				return this.resize(opath, npath, w, h);
			}
			else if(h == 0){
				double d = ArithHelper.div(w, ow);  指定尺寸/源尺寸 
				h = TypeChange.doubleToInt(ArithHelper.mul(oh, d));
			}
			else if(w == 0){
				double d = ArithHelper.div(h, oh);  指定尺寸/源尺寸 
				w = TypeChange.doubleToInt(ArithHelper.mul(ow, d));
			}
			if(w <= 0){
				w = TypeChange.doubleToInt(ow);
			}
			if(h <= 0){
				w = TypeChange.doubleToInt(oh);
			}
			return this.resize(opath, npath, w, h);
		} catch (MagickException e) {
			log.error(e.toString());
		}
		return false;
	}

	*//**
	 * 
	 * @param opath
	 *            源图片地址
	 * @param npath
	 *            新图片地址
	 * @param width
	 * @param height
	 *//*
	public boolean resize(String opath, String npath, int width, int height) {
		if(log.isDebugEnabled()){
			log.debug("...... #resize(...) opath: " + opath);
			log.debug("...... #resize(...) npath: " + npath);
		}
		ImageInfo info;
		try {
			System.setProperty("jmagick.systemclassloader", "no");
			info = new ImageInfo(opath);
			MagickImage image = new MagickImage(info);
			MagickImage scaled = image.scaleImage(width, height);
			if(scaled == null && log.isDebugEnabled()){
				log.error("...... MagickImage is null");
			}
			// 小图片文件的大小
			scaled.setFileName(npath);
			return scaled.writeImage(info);
		} catch (MagickException e) {
			log.error(e.toString());
		}
		return false;
	}

	@Override
	public boolean clip(BufferedInputStream bis, String filePathAndName,
			int w, int h, int x, int y, int x_w, int y_h) 
	{
		return false;
	}

	*//**
	 * 按坐标切割
	 *//*
	@Override
	public boolean incise(File file, String newfilepath, int w, int h,
			int x, int y, int x_w, int y_h) 
	{
		System.setProperty("jmagick.systemclassloader", "no");
		try {
			ImageInfo info = new ImageInfo(file.getPath());
			MagickImage image = new MagickImage(info);
			Rectangle rect = new Rectangle(x, y, w, h);
			MagickImage cropped = image.cropImage(rect);
			cropped.setFileName(newfilepath);
			cropped.writeImage(info);
			return true;
		} catch (MagickException e) {
			log.error(e.toString());
		}
		return false;
	}

	*//**
	 * 切割图片坐标 y = 0 , x 自动计算中间部分
	 * 
	 * @param path
	 * @param newfilepath
	 * @param w
	 * @param h
	 * @return
	 *//*
	@Override
	public boolean inciseTopAndCenter(String path, String newfilepath,
			int w, int h) 
	{
		System.setProperty("jmagick.systemclassloader", "no");
		int x = 0;
		int y = 0;
		ImageInfo info;
		try {
			info = new ImageInfo(path);
			MagickImage image = new MagickImage(info);
			Dimension dim = image.getDimension();
			int ow = TypeChange.doubleToInt(dim.getWidth());
			int oh = TypeChange.doubleToInt(dim.getHeight());
			x = (ow - w) / 2;
			if (x < 0) {
				x = 0;
			}
			ow = ow * (w / (w + h));
			oh = oh * (h / (w + h));
			Rectangle rect = new Rectangle(x, y, ow, oh);
			MagickImage cropped = image.cropImage(rect);
			cropped.setFileName(newfilepath);
			cropped.writeImage(info);
			 先切割成和指定高宽比一样形状的图片 
			 然后再按指定的高宽切割图片 
			ImageInfo ninfo = new ImageInfo(newfilepath);
			MagickImage nimage = new MagickImage(info);
			Rectangle nrect = new Rectangle(0, 0, w, h);
			MagickImage ncropped = nimage.cropImage(nrect);
			ncropped.setFileName(newfilepath);
			ncropped.writeImage(ninfo);
			return true;
		} catch (MagickException e) {
			log.error(e);
		}
		return false;
	}

	*//**
	 * @deprecated
	 *//*
	@Override
	public boolean narrowOrEnlarge(String opath, String npath, int w, int h) {
		// TODO Auto-generated method stub
		return false;
	}

	*//**
	 * @deprecated
	 *//*
	@Override
	public boolean watermark(String opath, String npath, String wpath) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean watermark(String opath, String npath, String wpath
			, int minwidth, int minheight) 
	{
		// TODO Auto-generated method stub
		return false;
	}*/
}
